FUNCTION YBC_SWS_LOGINORCHGPWD.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IV_USERNAME) TYPE  TEXT100
*"     VALUE(IV_PASSWORD) TYPE  TEXT100
*"     VALUE(IV_NEWPWD) TYPE  TEXT100 OPTIONAL
*"  EXPORTING
*"     VALUE(ES_USER) TYPE  YBCS_SWS_USER
*"----------------------------------------------------------------------
  DATA:
    LV_OK       TYPE C,
    LV_STR      TYPE STRING,
    LV_PASSWORD TYPE YBC_SWS_USER-PASSWORD,
    LV_USERID   TYPE YBC_SWS_USER-USERID.
  "密码转换
  DATA:
    LS_USER             LIKE ES_USER,
    LO_ENCRYPTOR        TYPE REF TO CL_HARD_WIRED_ENCRYPTOR,
    LO_CX_ENCRYPT_ERROR TYPE REF TO CX_ENCRYPT_ERROR,
    LV_IN_STRING        TYPE STRING,
    LV_OUT_STRING       TYPE STRING,
    LV_ERROR_MSG        TYPE STRING.

  CREATE OBJECT LO_ENCRYPTOR.

  LV_IN_STRING = IV_PASSWORD.
  TRY.
      CALL METHOD LO_ENCRYPTOR->ENCRYPT_STRING2STRING
        EXPORTING
          THE_STRING = LV_IN_STRING
        RECEIVING
          RESULT     = LV_OUT_STRING.
    CATCH CX_ENCRYPT_ERROR INTO LO_CX_ENCRYPT_ERROR.
      CALL METHOD LO_CX_ENCRYPT_ERROR->IF_MESSAGE~GET_TEXT
        RECEIVING
          RESULT = LV_ERROR_MSG.
      ES_USER-MSGTY = 'E'.
      MESSAGE S050(YBC_SWS) INTO ES_USER-MESSAGE WITH LV_ERROR_MSG.

  ENDTRY.

  IF ES_USER-MSGTY = 'E'.
    RETURN.
  ENDIF.

  CLEAR ES_USER.

  LV_USERID = IV_USERNAME.
  TRANSLATE LV_USERID TO UPPER CASE.

  IF LV_USERID = '###DEFAULTSAPUSER###'.
    LV_USERID = SY-UNAME.
  ENDIF.

  "按SAP账号找
  SELECT SINGLE *
    FROM YBC_SWS_USER
    INTO CORRESPONDING FIELDS OF LS_USER
    WHERE SAPID = LV_USERID.
  IF SY-SUBRC = 0.
    LV_OK = 'X'.
  ENDIF.

  "按外部账号查找
  SELECT SINGLE *
   FROM YBC_SWS_USER
   INTO CORRESPONDING FIELDS OF LS_USER
   WHERE EXTUSID = LV_USERID.
  IF SY-SUBRC = 0.
    LV_OK = 'X'.
  ENDIF.

*  "按Email找
*  IF LV_OK IS INITIAL.
*    SELECT SINGLE *
*      FROM YBC_SWS_USER
*      INTO CORRESPONDING FIELDS OF ES_USER
*      WHERE EMAIL = LV_USERID.
*    IF SY-SUBRC = 0.
*      LV_OK = 'X'.
*    ENDIF.
*  ENDIF.
*
*  "按Tel找
*  IF LV_OK IS INITIAL.
*    SELECT SINGLE *
*      FROM YBC_SWS_USER
*      INTO CORRESPONDING FIELDS OF ES_USER
*      WHERE TEL = LV_USERID.
*    IF SY-SUBRC = 0.
*      LV_OK = 'X'.
*    ENDIF.
*  ENDIF.
*
*  "按WECHAT找
*  IF LV_OK IS INITIAL.
*    SELECT SINGLE *
*      FROM YBC_SWS_USER
*      INTO CORRESPONDING FIELDS OF ES_USER
*      WHERE WECHAT = LV_USERID.
*    IF SY-SUBRC = 0.
*      LV_OK = 'X'.
*    ENDIF.
*  ENDIF.

  "按USERID找
  IF LV_OK IS INITIAL.
    SELECT SINGLE *
      FROM YBC_SWS_USER
      INTO CORRESPONDING FIELDS OF LS_USER
      WHERE USERID = LV_USERID.
    IF SY-SUBRC = 0.
      LV_OK = 'X'.
    ENDIF.
  ENDIF.

  IF LV_OK IS INITIAL.
    ES_USER-MSGTY = 'E'.
    MESSAGE S044(YBC_SWS) INTO ES_USER-MESSAGE WITH IV_USERNAME.
  ELSE.

    IF IV_NEWPWD IS NOT INITIAL."修改密码
      IF LS_USER-PASSWORD <> LV_OUT_STRING.
        ES_USER-MSGTY = 'E'.
        MESSAGE S052(YBC_SWS) INTO ES_USER-MESSAGE.
        RETURN.
      ENDIF.

      LV_IN_STRING = IV_NEWPWD.
      TRY.
          CALL METHOD LO_ENCRYPTOR->ENCRYPT_STRING2STRING
            EXPORTING
              THE_STRING = LV_IN_STRING
            RECEIVING
              RESULT     = LV_OUT_STRING.
        CATCH CX_ENCRYPT_ERROR INTO LO_CX_ENCRYPT_ERROR.
          CALL METHOD LO_CX_ENCRYPT_ERROR->IF_MESSAGE~GET_TEXT
            RECEIVING
              RESULT = LV_ERROR_MSG.
          ES_USER-MSGTY = 'E'.
          MESSAGE S050(YBC_SWS) INTO ES_USER-MESSAGE WITH LV_ERROR_MSG.

      ENDTRY.

      IF ES_USER-MSGTY = 'E'.
        RETURN.
      ENDIF.

      LV_PASSWORD  = LV_OUT_STRING.

      UPDATE YBC_SWS_USER
        SET PASSWORD = LV_PASSWORD
            PWD_INIT = SPACE
            LAUSER = SY-UNAME
            LADATE = SY-DATUM
            LATIME = SY-UZEIT
      WHERE USERID = LS_USER-USERID
        AND EXTUSID = LS_USER-EXTUSID
        AND SAPID = LS_USER-SAPID.
      COMMIT WORK.

      ES_USER-MSGTY = 'S'.
      MESSAGE S053(YBC_SWS) INTO ES_USER-MESSAGE.

    ELSE. "登陆

      IF LS_USER-PASSWORD <> LV_OUT_STRING.
        ES_USER-MSGTY = 'E'.
        MESSAGE S051(YBC_SWS) INTO ES_USER-MESSAGE.
        RETURN.
      ENDIF.

      ES_USER = LS_USER.

      TYPES:
        BEGIN OF TY_SUBCATEGORIES,
          KEY          TYPE STRING,
          SUBCATEGORY1 TYPE YBC_SWS_REQUEST-SUBCATEGORY1,
          SUBCATEGORY2 TYPE YBC_SWS_REQUEST-SUBCATEGORY2,
          SUBCATEGORY3 TYPE YBC_SWS_REQUEST-SUBCATEGORY3,
        END OF TY_SUBCATEGORIES.

      DATA:
        LS_SUBCAT TYPE TY_SUBCATEGORIES,
        LT_SUBCAT TYPE STANDARD TABLE OF TY_SUBCATEGORIES.

      SELECT
        SUBCATEGORY1
        SUBCATEGORY2
        SUBCATEGORY3
        INTO CORRESPONDING FIELDS OF TABLE LT_SUBCAT
        FROM YBC_SWS_REQUEST
        JOIN YBC_SWS_APPROVER
          ON YBC_SWS_REQUEST~REQUESTID = YBC_SWS_APPROVER~REQUESTID
       WHERE YBC_SWS_APPROVER~APPROVERID = ES_USER-USERID
         AND YBC_SWS_REQUEST~MOD = 'NEW'.

      SORT LT_SUBCAT BY SUBCATEGORY1 SUBCATEGORY2 SUBCATEGORY3.
      DELETE ADJACENT DUPLICATES FROM LT_SUBCAT.

      DELETE LT_SUBCAT WHERE SUBCATEGORY1 IS INITIAL AND SUBCATEGORY2 IS INITIAL AND SUBCATEGORY3 IS INITIAL.
      LOOP AT LT_SUBCAT INTO LS_SUBCAT.
        LS_SUBCAT-KEY = 'SUBCATKEY' && SY-TABIX.
        MODIFY LT_SUBCAT FROM LS_SUBCAT.
      ENDLOOP.

      "SwiftSAP Host
      SELECT SINGLE
        VALUE
        INTO ES_USER-SWIFTSAPHOST
        FROM YBC_SWS_CONTR
       WHERE PARAM IN ('SWIFTSAPHOST', 'SWIFTSAP_HOST')
         AND ACTIVE = 'X'.

      CONDENSE ES_USER-SWIFTSAPHOST.

      ES_USER-SUBCATEGORIES = /UI2/CL_JSON=>SERIALIZE( DATA = LT_SUBCAT ).

    ENDIF.
  ENDIF.
ENDFUNCTION.
